package com.squirrel.serialportcore.machine.init.config;

import com.squirrel.serialportcore.common.CommonBase;
import com.squirrel.serialportcore.entity.mapper.PortQueueMapper;
import com.squirrel.serialportcore.machine.consumer.QueueConsumer;
import com.squirrel.serialportcore.machine.init.after.QueueInitializer;
import com.squirrel.serialportcore.machine.init.after.SerialPortInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;


/**
 * RabbitMQ配置
 *
 * @author Squbi
 * @date 2021/3/16 16:20
 */
@Configuration
@EnableConfigurationProperties(RabbitProperties.class)
public class RabbitCoreConfig {
    private static final Logger logger = LoggerFactory.getLogger(RabbitCoreConfig.class);

    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public RabbitAdmin rabbitAdmin(RabbitTemplate rabbitTemplate) {
        return new RabbitAdmin(rabbitTemplate);
    }

    @Bean
    public boolean isInit(SerialPortInitializer serialPortInitializer, QueueInitializer queueInitializer) {
        logger.info(" --- 消息队列初始化阶段 ---");
        Set<String> keySet = CommonBase.getJsonProperty().getPortProperty().keySet();
        for (String portName : keySet) {
            queueInitializer.initQueue(portName);
            logger.info(" - 消息队列 {} 已完成初始化 -", portName);
        }
        return true;
    }

    @Bean
    @Order(Ordered.LOWEST_PRECEDENCE)
    public SimpleMessageListenerContainer simpleMessageListenerContainer(QueueConsumer consumer, ConnectionFactory connectionFactory) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        //初始化消费者
        container.setConcurrentConsumers(1);
        //最多消费者
        container.setMaxConcurrentConsumers(5);
        // 指定监听的队列s
        ArrayList<String> inQs = new ArrayList<>();
        Map<String, PortQueueMapper> portQueue = CommonBase.getPortQueueName();
        for (Map.Entry<String, PortQueueMapper> entry : portQueue.entrySet()) {
            inQs.add(entry.getValue().getInQ());
        }
        String[] inq = inQs.toArray(new String[0]);
        container.setQueueNames(inq);
        //消费者主动确认
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        container.setExposeListenerChannel(true);
        //指定消息监听对象
        container.setMessageListener(consumer);
        return container;
    }
}
